package com.runachina.othercompany;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.runachina.R;
import com.runachina.common.BaseActivity;
import com.runachina.common.CompanyActivity;
import com.runachina.java.SendDataAnalysis;
import com.runachina.java.TcpSocket;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 读取其它厂家的表
 */



public class OtherCompanyActivity extends BaseActivity {

    private EditText inputnum;
    private Button btnread;
    private TextView sendtext;
    public  static TextView rcvtext,rcvbyte;

    private static String ifNumOver="over?yes";

    //报文
    //√ 震宇 FEFEFE682076280311 0000000103901F00ED16
    private static byte [] zhenyuMeter  = {(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68,0x20,
            (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,//地址倒序发送
            0x00, 0x00, 0x00, 0x01, 0x03,
            (byte) 0x90,(byte) 0x1f,(byte) 0x00,
            (byte) 0x00,0x16,
            (byte) 0x00,0x16
    };
    //√ 胜利68 20 82 17 31 30 00 00 00 01 03 90 1f 00 35 16
    private static byte [] shengliMeter = {(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68,0x20,
            (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,//地址倒序
            0x00, 0x00, 0x00, 0x01, 0x03,
            (byte) 0x90,(byte) 0x1f,(byte) 0x00,
            (byte) 0x35,0x16,
            (byte) 0x00,0x16
    };
    //√ 力创 FEFEFE6820 04071001 0059420103901F00F216
    private static byte [] lichuangMeter = {(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68,0x20,
            (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,//地址顺序
            0x00, 0x59, 0x42, 0x01, 0x03,(byte) 0x90,(byte) 0x1f,(byte) 0x00,
            (byte) 0x00,0x16,
            (byte) 0x00,0x16
    };


    //？ 华仪乐业 68  20  B0  B1  B2  B3  B4  B5  B6  01 03 C3 52  34  CS  16
    private static byte [] huayileMeter = {(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68,0x20,
            (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,//地址没说
            0x01, 0x03, (byte)0xc3, 0x52, 0x34,
            (byte) 0x00,0x16,
            (byte) 0x00,0x16
    };
    //√ 迈拓68 20 12 34 56 78 00 11 11 01 03 90 1F 00 71 16
    private static byte [] maituoMeter = {(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68,0x20,
            (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,//地址顺序
            (byte) 0x00,(byte) 0x11,(byte) 0x11,
            0x01, 0x03, (byte)0x90, 0x1f, 0x00 ,
            (byte) 0x00,0x16,
            (byte) 0x00,0x16
    };
    //√ 天罡68 20 12 34 56 78 00 11 11 01 03 1F 90 00 71 16
    private static byte [] tiangangMeter = {(byte) 0xFE,(byte) 0xFE,(byte) 0xFE,0x68,0x20,
            (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,//地址倒序
            (byte) 0x00,(byte) 0x11,(byte) 0x11,
            0x01, 0x03, (byte)0x1f, (byte) 0x90, 0x00 ,
            (byte) 0x00,0x16,
            (byte) 0x00,0x16
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_readothercompany);

        set_toolbar(companyName + "/" + readMethod, "");

        set_leftmenu(OtherCompanyActivity.this,OtherCompanyActivity.class);

        init_view();

        inWhichAc="othercompany.OtherCompanyActivity";
    }



    /**
     * 初始化界面控件
     */
    void init_view(){
        inputnum=(EditText)findViewById(R.id.inputnum);
        btnread=(Button)findViewById(R.id.btnread);
        btnread.setOnClickListener(readMeter);

        sendtext=(TextView)findViewById(R.id.sendtext);
        rcvtext =(TextView)findViewById(R.id.rcvtext);
        rcvbyte =(TextView)findViewById(R.id.rcvbyte);
    }
    private void clear_textDisplay(){
        sendtext.setText("");
        rcvtext.setText("");
        rcvbyte.setText("");

    }

    /**
     * 按键响应事件：点击读取抄表
     * 显示发送出去的数据
     */
    OnClickListener readMeter = new OnClickListener() {
        @Override
        public void onClick(View v) {
            clear_textDisplay();
            //获取完整的发送sendbyte[]
            String meterNum=inputnum.getText().toString();

            if(meterNum.length()>=8){
                startTimer(TI);
                byte[] baowen= getChoiceBaowen();
                byte[] sendbyte= SendDataAnalysis.otherCompanyRead(baowen, meterNum, ifNumOver, 5, 8);
                //显示发送报文
                sendtext.setText(byteToHexString(sendbyte));
                //发送sendbyte[]
                String sendResult = TcpSocket.wifiSendByte(sendbyte);
//                new Timer().schedule(new TimerTask() {
//                    @Override
//                    public void run() {
//                        TcpSocket.receServerMessage();
//                    }
//                },3000);
            }else{
                show("表号错误!");
            }

        }
    };

    private byte[] getChoiceBaowen() {
        byte baowen[]=readmeter;
        switch (companyName) {
            case "震宇--抄表":
                baowen = zhenyuMeter;
                ifNumOver="over?yes";
                break;
            case "胜利--抄表":
                baowen = shengliMeter;
                ifNumOver="over?yes";
                break;
            case "力创--抄表":
                baowen = lichuangMeter;
                ifNumOver="over?no";
                break;
            /*case "兰吉尔--抄表":
                baowen = lanjierMeter;
                ifNumOver="over?no";
                break;
            case "汇中--抄表":
                baowen = huizhongMeter;
                ifNumOver="over?no";
                break;
             case "伟岸--抄表":
                baowen = weianMeter;
                ifNumOver="over?no";
                break;*/
            case "华仪乐--抄表":
                baowen = huayileMeter;
                ifNumOver="over?no";
                break;
            case "迈拓--抄表":
                baowen = maituoMeter;
                ifNumOver="over?no";
                break;
            case "天罡--抄表":
                baowen = tiangangMeter;
                ifNumOver="over?yes";
                break;
            default:
                break;
        }
        return baowen;
    }

    /**
     * Toolbar中的Menu菜单项
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    /**
     * Activity创建时候会执行一次，所以可以用来初始化toolbar中的wifi图标
     * @return
     */
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        MenuItem menuItem = menu.findItem(R.id.menu_connect);
        //wifi connect，返回当前连接状态
        String connectResult= TcpSocket.readConnect();
        if((connectResult.equals("连接成功!"))||connectResult.equals("已经连接!")){
            menuItem.setTitle("connect!");
            menuItem.setIcon(R.drawable.wifi_connect);
        }else{
            menuItem.setTitle("no connect!");
            menuItem.setIcon(R.drawable.wifi_noconnect);
        }
        return true;
    }
    private MenuItem me;
    private int type=0;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        me=item;
        System.out.println("onOptionsItemSelected");

        switch (item.getItemId()){
            case R.id.menu_connect:
                String connectResult= TcpSocket.wificonnect();//wifi connect
                show(connectResult);//TOast connect result
                if((connectResult.equals("连接成功!"))||connectResult.equals("已经连接!")){
                    item.setTitle("connect!");
                    item.setIcon(R.drawable.wifi_connect);
                }else{
                    item.setTitle("no connect!");
                    item.setIcon(R.drawable.wifi_noconnect);
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            goWifiSetting();
                            type=1;
                        }
                    },1000);
                }
                return true;
            case R.id.menu_choice:
                intent = new Intent(OtherCompanyActivity.this, CompanyActivity.class);
                startActivity(intent);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    //每次进来判断是否是从设置wifi页面进来的，如果是，重新连接socket，判断是否可用
    @Override
    protected void onResume() {
        super.onResume();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                type=0;
                String connectResult = TcpSocket.wificonnect();//wifi connect
                //show(connectResult);//TOast connect result
                if (isWifi()) {
                    if (me != null) {
                        if ((connectResult.equals("连接成功!")) || connectResult.equals("已经连接!")) {

                            me.setTitle("connect!");
                            me.setIcon(R.drawable.wifi_connect);
                        } else {

                            me.setTitle("no connect!");
                            me.setIcon(R.drawable.wifi_noconnect);
                        }
                    }
                } else {
                    if (me!=null){
                        me.setTitle("no connect!");
                        me.setIcon(R.drawable.wifi_noconnect);
                    }

                }
            }
        });
    }
    /**
     * 设置back监听
     */
    @Override
    public void onBackPressed() {
        exitApp();
    }
}
